package com.aletto.general_store.ui.adapters;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.ConsumerOrder;
import com.aletto.general_store.data.model.OrderItem;
import com.aletto.general_store.data.model.StoreOrder;
import com.aletto.general_store.presenters.OrderRaiseArbitrationTask;
import com.aletto.general_store.presenters.OrderRefundTask;
import com.aletto.general_store.presenters.OrderTakeFundTask;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.ui.activities.OrderCommentActivity;
import com.aletto.general_store.ui.activities.orders.OrderCompanyPopup;
import com.aletto.general_store.ui.activities.orders.OrderConsumerDeliveryActivity;
import com.aletto.general_store.ui.activities.orders.OrderFormActivity;
import com.aletto.general_store.ui.activities.orders.OrderStoreAfterSaleActivity;
import com.aletto.general_store.ui.activities.orders.OrderStoreCompleteActivity;
import com.aletto.general_store.ui.activities.orders.OrderStoreDeliveryActivity;
import com.aletto.general_store.ui.activities.orders.OrderStoreShippedActivity;
import com.aletto.general_store.utils.ToastUtils;
import com.aletto.general_store.utils.image.GlideApp;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.ToastView;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class StoreOrderAdapter extends BaseMultiItemQuickAdapter<StoreOrder, BaseViewHolder> {

    public StoreOrderAdapter() {
        addItemType(StoreOrder.DELIVERY_TYPE, R.layout.item_store_order_delivery);
        addItemType(StoreOrder.SHIPPED_TYPE, R.layout.item_store_order_shipped);
        addItemType(StoreOrder.AFTER_SALE_TYPE, R.layout.item_store_order_after_sale); // 退款
        addItemType(StoreOrder.COMPLETE_TYPE, R.layout.item_store_order_complete_type);

        setOnItemClickListener((adapter, view, position) -> {
            Context context = view.getContext();
            StoreOrder item = (StoreOrder) adapter.getItem(position);

            switch (item.getItemType()) {
                case StoreOrder.DELIVERY_TYPE: // 待发货
                    context.startActivity(OrderStoreDeliveryActivity.newIntent(context, item));
                    break;

                case StoreOrder.SHIPPED_TYPE: // 已发货
                    context.startActivity(OrderStoreShippedActivity.newIntent(context, item));
                    break;

                case StoreOrder.AFTER_SALE_TYPE:
                    context.startActivity(OrderStoreAfterSaleActivity.newIntent(context, item));
                    break;

                case StoreOrder.COMPLETE_TYPE:
                    context.startActivity(OrderStoreCompleteActivity.newIntent(context, item));
                    break;
            }
        });

    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, StoreOrder item) {
        Context context = getContext();

        // 状态
        TextView tv_order_status = holder.getView(R.id.tv_order_status);
        tv_order_status.setText(getStatusString(item.getStatus()));

        BigDecimal price = new BigDecimal(item.getPrice()).multiply(new BigDecimal(item.getNum()));

        holder.setText(R.id.tv_goods_title, item.getGoods_name())
                .setText(R.id.tv_sold_number, "× " + item.getNum())
                .setText(R.id.tv_goods_price, "已付：" + price + " USDT");

        // 商品图片
        List<String> goodsImg = item.getGoods_img();
        if (goodsImg != null && !goodsImg.isEmpty()) {
            ImageView iv_goods_cover = holder.getView(R.id.iv_goods_cover);
            GlideApp.with(iv_goods_cover).load(goodsImg.get(0)).normal().into(iv_goods_cover);
        }

        switch (item.getItemType()) {
            case StoreOrder.DELIVERY_TYPE:
                View tv_store_delivery = holder.getView(R.id.tv_store_delivery);

                if (item.getGoods_type() == 2) { // 实物
                    tv_store_delivery.setOnClickListener(v -> context.startActivity(OrderFormActivity.newIntent(context, item.getOrderId())));

                } else {
                    tv_store_delivery.setOnClickListener(v -> task(item).execute());
                }

                break;

            case StoreOrder.AFTER_SALE_TYPE:
                View tv_confirm = holder.getView(R.id.tv_confirm);
                if (item.getStatus() == 7 || item.getStatus() == 10) {
                    holder.setVisible(R.id.tv_store_customer, false)
                            .setText(R.id.tv_store_delivery, R.string.order_service)
                            .setText(R.id.tv_logistics, R.string.order_store_customer)
                            .setText(R.id.tv_confirm, R.string.order_logistics);

                } else {

                    holder.setText(R.id.tv_store_customer, R.string.order_service)
                            .setText(R.id.tv_store_delivery, R.string.order_store_customer)
                            .setText(R.id.tv_logistics, R.string.order_logistics)
                            .setText(R.id.tv_confirm, R.string.appeal_order_refund_agree);
                }

                tv_confirm.setOnClickListener(v -> {
                    if (item.getStatus() == 7 || item.getStatus() == 10) return;

                    new OrderRefundTask(item.getBtc_order_id()).execute(); // 同意退款
                });

                break;

            case StoreOrder.COMPLETE_TYPE:

                if (item.getStatus() == 8) { // 签收

                    holder.setText(R.id.tv_store_customer, R.string.order_service)
                            .setText(R.id.tv_store_delivery, R.string.order_store_customer)
                            .setText(R.id.tv_logistics, R.string.order_logistics)
                            .setText(R.id.tv_confirm, R.string.order_store_take_fund);

                    holder.getView(R.id.tv_confirm).setOnClickListener(v -> { // 提现货款
                                if (item.getMature_time() * 1000 <= new Date().getTime()) {
                                    new OrderTakeFundTask(item.getBtc_order_id()).execute();

                                } else {
                                    ToastUtils.makeText("未到提现时间");
                                }
                            }
                    );

                } else {

                    holder.setVisible(R.id.tv_store_customer, false)
                            .setText(R.id.tv_store_delivery, R.string.order_service)
                            .setText(R.id.tv_logistics, R.string.order_store_customer)
                            .setText(R.id.tv_confirm, R.string.order_logistics);

                    holder.getView(R.id.tv_confirm).setOnClickListener(v -> {
                    });
                }

                break;
        }
    }

    private String getStatusString(int status) {
        // 1下单，2支付成功，3已发货，4途中，5申请退货，6退货中 7退货成功 8已签收 9已完成 10拒绝退款
        switch (status) {
            case 1:
                return "下单";
            case 2:
                return "支付成功";
            case 3:
                return "已发货";
            case 4:
                return "途中";
            case 5:
                return "买家申请退款";
            case 6:
                return "退货中";
            case 7:
                return "买家退款成功";
            case 8:
                return "买家已签收,交易成功";
            case 9:
                return "交易成功";
            case 10:
                return "拒绝退款";
        }

        return "未知状态";
    }

    // 发货任务
    private SimpleTemplate<Object> task(StoreOrder item) {
        return new SimpleTemplate<>() {

            @Override
            public INetTask.View<Object> view() {
                return new ToastView<>(getContext()) {
                    @Override
                    public void success(Object data) {
                        remove(item);
                    }
                };
            }

            @Override
            public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
                return new NetSingleTask<>(DataRepository.getInstance().sendOrder(item.getOrderId(), null, null));
            }
        };
    }

}
